package com.shopping.mall.core.module.instrumentPanel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shopping.mall.base.util.DateUtils;
import com.shopping.mall.core.module.instrumentPanel.entity.*;
import com.shopping.mall.core.module.instrumentPanel.service.InstrumentPanelService;
import com.shopping.mall.core.module.ipAddress.entity.IpAddress;
import com.shopping.mall.core.module.ipAddress.mapper.IpAddressMapper;
import com.shopping.mall.core.module.order.entity.Order;
import com.shopping.mall.core.module.order.mapper.OrderMapper;
import com.shopping.mall.core.module.saleData.mapper.SaleDataMapper;
import com.shopping.mall.core.module.user.entity.User;
import com.shopping.mall.core.module.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

@Service
public class InstrumentPanelServiceImpl implements InstrumentPanelService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private SaleDataMapper saleDataMapper;
    @Autowired
    private IpAddressMapper ipAddressMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 查询订单数量
     * @return
     */
    @Override
    public OrderNum getOrderNum() throws ParseException {
        OrderNum orderNum=new OrderNum();
        QueryWrapper<Order> queryWrapper=new QueryWrapper<>();
        int count=orderMapper.selectCount(queryWrapper);
        orderNum.setAllNum(count);
        List<OrderStateNum> orderStateNumList=orderMapper.getStateNum();
        orderNum.setOrderStateNum(orderStateNumList);
        queryWrapper.ge("create_at", new Timestamp(DateUtils.getTodayDate().getTime()));
        orderNum.setTodayNum(orderMapper.selectCount(queryWrapper));
        orderNum.setHourOrderNums(orderMapper.getHourOrderNum(DateUtils.getBeforeDayTimestamp(new Timestamp(new Date().getTime()))));
        return orderNum;
    }




    /**
     * 查询访问量
     */
    @Override
    public VisitNum getAllVisitNum() throws ParseException {
        VisitNum visitNum=new VisitNum();
        visitNum.setAllVisitNum(ipAddressMapper.selectCount(new QueryWrapper<>()));
        QueryWrapper<IpAddress> queryWrapper=new QueryWrapper<>();
        queryWrapper.ge("create_at", new Timestamp(DateUtils.getTodayDate().getTime()));
        visitNum.setTodayVisitNum(ipAddressMapper.selectCount(queryWrapper));
        visitNum.setHourVisitNumList(ipAddressMapper.getHourVisitNum(DateUtils.getBeforeDayTimestamp(new Timestamp(new Date().getTime()))));
        return visitNum;
    }

    @Override
    public PayNum getPrice() throws ParseException {
        PayNum payNum=orderMapper.getPayNum();
        payNum.setTodayPrice(orderMapper.getTodayPrice(new Timestamp(DateUtils.getTodayDate().getTime())));
        return payNum;
    }

    @Override
    public UserNum getUserNum() throws ParseException {
        UserNum userNum=new UserNum();
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        userNum.setTotalUserNum(userMapper.selectCount(queryWrapper));
        queryWrapper.ge("create_at",new Timestamp(DateUtils.getTodayDate().getTime()));
        userNum.setTodayUserNum(userMapper.selectCount(queryWrapper));
        return userNum;
    }

}
